package com.etc.controller.demo;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.annotation.LogOperation;
import com.etc.common.result.ResultMapper;
import com.etc.demo.service.IDemoService;
import com.etc.demo.vo.DemoVO;

/**
 * @Title: DemoController.java
 * @Package com.etc.demo.controller
 * @Description: rest风格的controller
 * @author 黄宇
 * @date 2016年3月19日 下午11:18:46
 * @version V1.0
 */
@Controller
@RequestMapping("/demo")
public class DemoController {

	@Autowired
	private IDemoService demoService;
	
	@LogOperation
	@ResponseBody
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public Map<String, Object> deleteDemo(@PathVariable Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		this.demoService.delete(id);
		return ResultMapper.convertSuccess();
	}
	
	@LogOperation
	@ResponseBody
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Map<String, Object> getDemo(@PathVariable Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		DemoVO vo = this.demoService.selectById(id);
		return ResultMapper.convertSuccess(vo);
	}

	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Map<String, Object> ListDemo(Map<String, Object> map, HttpServletRequest request,
			HttpServletResponse response) {
		List<DemoVO> voList = this.demoService.list(map);
		return ResultMapper.convertSuccess(voList);
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public Map<String, Object> updateDemo(DemoVO vo, HttpServletRequest request,
			HttpServletResponse response) {
		this.demoService.update(vo);

		return ResultMapper.convertSuccess();
	}

	@ResponseBody
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public Map<String, Object> insertDemo(DemoVO vo, HttpServletRequest request,
			HttpServletResponse response) {
		this.demoService.insert(vo);

		return ResultMapper.convertSuccess();
	}

}
